<?php
class BaseDB
{
	protected $conf = null;
	protected $db = null;
	protected $table = '';
	
	function __construct()
	{
		$this->conf = ConfigUtil::getConfig('EXTERNALS');
		try{
			$this->db = QFrameDB::getInstance($this->conf['DBConf']);
		}catch(QFrameDBException $e){
			var_dump($e->getMessage());
		}
	}
	
	public function insert($data)
	{
		$data['create_time'] = date("Y-m-d H:i:s");
		return $this->db->insert($this->table, $data);
	}
	
	public function update($data, $where)
	{
		if (!is_array($data) || empty($data) || !is_array($where) || empty($where))
			return false;
			
		list($condition, $whereData) = $this->_getWhere($where);
		return $this->db->update($this->table, $data, $condition, $whereData);
	}
	
	public function delete($where)
	{
		if (!is_array($where) || empty($where))
			return false;
		
		list($condition, $whereData) = $this->_getWhere($where);
		return $this->db->delete($this->table, $condition, $whereData);
	}
	
	public function deleteByIds($ids)
	{
		if (!is_array($ids) || empty($ids))
			return false;

		$condition = "id=?";
		for($cnt=count($ids); $cnt>1; $cnt--) {
			$condition .= " or id=?";
		}
		$sql = "delete from " . $this->table . " where " . $condition;
		return $this->db->execute($sql, $ids, false);
	}
	
	public function select($where, $start=0, $count=1)
	{
		if (empty($where) || !is_array($where)) 
		{
			$where = array("1"=>"1");
		}
		$start = max(0, (int)$start);
		$count = max(1, (int)$count);
		
		list($condition, $whereData) = $this->_getWhere($where);
		$sql = "select * from " . $this->table . " where " . $condition . " order by id desc limit " . $start . "," .$count;
		return $this->db->getAll($sql, $whereData);
	}
	
	public function getCount($where) {
		if (empty($where) || !is_array($where)) {
			$where = array("1"=>"1");
		}
		
		list($condition, $whereData) = $this->_getWhere($where);
		$sql = "select count(id) from " . $this->table . " where " . $condition;
		return $this->db->getOne($sql, $whereData);
	}
	
	public function getXByIds($ids)
	{
		$old_ids = $ids;
		$ids = is_array($ids) ? $ids : array($ids);
		$sql = "select * from " . $this->table . " where id in (" . $this->getSelectInRangeStr($ids) . ")";
		$res = $this->db->getAll($sql, $ids);

		$ret = array();
		if (is_array($old_ids))
		{
			return array('total' => count($res), 'data' => $res);
		}
		else
		{
			return array('total' => 1, 'data' => $res[0]);
		}
	}
	
	protected function getSelectInRangeStr($range=array())
	{
		if (empty($range))
			return '';
		
		$range = is_array($range) ? $range : array($range);
		$str = '';
		$count = count($range);
		for($i=0; $i<$count; $i++)
		{
			$str .= ' ?, ';
		}
		$str = substr(trim($str), 0, -1);
		return $str;
	}
	
	protected function _getWhere($where)
	{
		$conditions = array();
		$whereData = array();
		foreach($where as $k=>$v)
		{
			$conditions[] = $k . '=?';
			$whereData[] = $v;
		}
		return array(implode(' and ', $conditions), $whereData);
	}
	
	public function modifyById($data, $id) {
		if (empty($data) || empty($id))return false;
		return $this->update($data, array('id'=>$id));
	}
}
?>